/*
 * TITLE:ResponseFromServer
 * AUTHOR:Manori Thakur
 * PURPOSE: Parent class for all responses
 */
package clientresponse;

import java.io.DataInputStream;
import java.io.IOException;

import android.util.Log;

public class ResponseFromServer {
	private static final String TAG = ResponseFromServer.class.getSimpleName();
    protected DataInputStream inFromServer = null;

    public short process() {
         return -2;
    }

    public void setIn(DataInputStream receiveServer) {
        this.inFromServer = receiveServer;
    }
    
    protected short readShort(DataInputStream inFromServer) {
    	try {
			return (short)((inFromServer.readByte()& 0xFF)+(inFromServer.readByte()<< 8));
		} catch (IOException e) {
			Log.e(TAG,"IOException in method readShort()"+e.toString());
		}
		return -1;
    }
    
    protected int readInt(DataInputStream inFromServer) {
    	try {
			return (int)((inFromServer.readByte()& 0xFF)+(inFromServer.readByte()<< 8)+(inFromServer.readByte()<< 16)+(inFromServer.readByte()<< 24));
		} catch (IOException e) {
			Log.e(TAG,"IOException in method readInt()"+e.toString());
		}
		return -1;
    }
    
    protected boolean readBoolean(DataInputStream inFromServer){
    	try {
			return inFromServer.readBoolean();
		} catch (IOException e) {
			Log.e(TAG,"IOException in method readBoolean()"+e.toString());
		}
		return false;
    }
    
    protected String readString(DataInputStream inFromServer){
    	try {
    		 int length = (int)readShort(inFromServer);
             byte[]b = new byte[length];
             inFromServer.read(b,0,length);
             return new String(b,0,length);
    	}catch (IOException e) {
			Log.e(TAG,"IOException in method readString()"+e.toString());
		}
    	return null;
    }

}
